﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class admin_workscategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            BindWorksCategoryInfoList();
            BindNotice();
        }
        private void BindWorksCategoryInfoList()
        {
            List<WorksCategoryInfo> list = WorksCategoryInfoLogic.GetNowPageWorksCategoryInfo(20, 1);
            CategoryListRpt.DataSource = list;
            CategoryListRpt.DataBind();
        }
        private void BindNotice()
        {
            noticefailed.Visible = false;
            noticesuccess.Visible = false;
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {

        }
        protected void ibtnDelete_OnClick(object sender, EventArgs e)
        {
            ImageButton ib = new ImageButton();
            int CategoryID = 0;
            try
            {
                ib = (ImageButton)sender;
                CategoryID = Int32.Parse(ib.ToolTip.ToString());
            }
            catch {
                return;
            }
            int worksCount = WorksInfoLogic.GetPageCount(1, CategoryID);

            if (worksCount == 0 && WorksCategoryInfoLogic.DeleteWorksCategoryInfo(CategoryID) == 1)
            {
                ChangeStatus(true);
            }
            else
            {
                ChangeStatus(false);
            }
            if (worksCount > 0)
            {
                noticefailedtext.InnerText = "分类中仍有案例，请先删除指定分类中所有的案例";
            }
            else {
                noticefailedtext.InnerText = "分类删除失败！请重试！";
            }

            //重新绑定数据
            BindWorksCategoryInfoList();
        }

        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefailed.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();", true);
        }
    }
}
